package com.test.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.test.model.BaseCode;
import com.test.service.Impl.CodeServiceImpl;

@Component
public class CodeUtil {
	
	private static CodeServiceImpl codeServiceImpl;

	@Autowired
	public void setCodeServiceImpl(CodeServiceImpl codeServiceImpl) {
		CodeUtil.codeServiceImpl = codeServiceImpl;
	}
	 	/**

	 * 获取缓存的编码集合对象中的编码对象

	 */

	
	@SuppressWarnings("unchecked")
	public static <E extends BaseCode> E getCodeEntity(String entityName, String code) {
		return (E) codeServiceImpl.get(entityName, code);
	}
	
	/**
	 * 获取缓存的编码集合对象中的编码对象编码名称 兼容多个code逗号隔开的情况
	 */
	public static <E extends BaseCode> String getCodeName(String entityName, String code) {
		if (StringUtils.isBlank(code)) {
			return "";
		}
		String[] codes = new String[] { code };
		if (code.contains(",")) {
			codes = code.split(",");
		}
		StringBuilder sb = new StringBuilder();
		for (String c : codes) {
			E codeEntity = getCodeEntity(entityName, c);
			if (codeEntity != null) {
				sb.append(codeEntity.getName() + ",");
			}
		}
		if (sb.length() > 0) {
			return sb.deleteCharAt(sb.length() - 1).toString();
		}
		return "";
	}
	
}
